package com.foreverwin.jzymes.report.grind.service.impl;

import com.foreverwin.jzymes.common.domain.ActivityLog;
import com.foreverwin.jzymes.common.mapper.NCCodeAdviceMapper;
import com.foreverwin.jzymes.common.service.INCCodeAdviceService;
import com.foreverwin.jzymes.common.service.ISiteAdviceService;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.production.grind.domain.GrindCheck;
import com.foreverwin.jzymes.production.grind.domain.GrindCheckNc;
import com.foreverwin.jzymes.report.grind.mapper.GrindLogReportMapper;
import com.foreverwin.jzymes.report.grind.service.IGrindLogReportService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class GrindLogReportServiceImpl implements IGrindLogReportService {
    @Autowired
    GrindLogReportMapper grindLogReportMapper;
    @Autowired
    ISiteAdviceService siteAdviceService;
    @Autowired
    NCCodeAdviceMapper ncCodeAdviceMapper;
    @Override
    public List<ActivityLog> selectgrindOut(Map< String ,Object > jsonMap) {

       String site =  ( String )jsonMap.get( "site" );
       siteAdviceService.existSite( site );
      /* String machineType = ( String )jsonMap.get( "machineType" );*/
       String itemBo  = ( String )jsonMap.get( "itemBo" );

        Date startDate = null;
        Date endDate = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if ( (String) jsonMap.get( "startDate" ) != "" ){
            try {
                startDate = sdf.parse( (String) jsonMap.get( "startDate" ) );
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        if ( (String) jsonMap.get( "endDate" ) != "" ){
            try {
                endDate = sdf.parse( (String) jsonMap.get( "endDate" ) );
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

       List<ActivityLog> logList = grindLogReportMapper.selectActivityLogByReport( site , "" ,itemBo ,startDate ,endDate );

       return  logList;
    }

    /**
     *
     * @param jsonMap
     *
     * @return
     */
    @Override
    public List<GrindCheck> selectgrindCheck(Map<String, Object> jsonMap) {
        String site =  ( String )jsonMap.get( "site" );
        siteAdviceService.existSite( site );
        String itemBo  = ( String )jsonMap.get( "itemBo" );

        Date startDate = null;
        Date endDate = null;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        if (  !StringUtils.isEmpty((String) jsonMap.get( "startDate" ))){
            try {
                startDate = sdf.parse( (String) jsonMap.get( "startDate" ) );
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        if ( !StringUtils.isEmpty( (String) jsonMap.get( "endDate" ) )){
            try {
                endDate = sdf.parse( (String) jsonMap.get( "endDate" ) );
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        List<GrindCheck> grindChecks = grindLogReportMapper.selectGrindCheckByReport( site , itemBo ,startDate,endDate );

        return grindChecks;
    }

    @Override
    public List<Map<String, Object>> selectgrindCheckNC(Map<String, Object> jsonMap) {
        String site = CommonMethods.getSite();
        String handle = (String)jsonMap.get("handle");

        Date createdDateTime = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (  !StringUtils.isEmpty((String)jsonMap.get("createdDateTime"))){
            try {
                createdDateTime = simpleDateFormat.parse((String)jsonMap.get("createdDateTime"));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        List<GrindCheckNc> grindCheckNcs = grindLogReportMapper.selectgrindCheckNCByReport( handle );
        List<Map<String , Object> >  list = new ArrayList<>();
        for ( GrindCheckNc fqcCheckNc : grindCheckNcs ){
            String handl = BOHandleUtil.buildNcCodeHandle( site, fqcCheckNc.getNcCode() );
            Map<String,Object> ncCodeMap = ncCodeAdviceMapper.selectNCCodeMapById( handl );
            Map<String,Object> map = new HashMap<>();
            map.put("ncCode" , fqcCheckNc.getNcCode());
            map.put("ncDesc", ncCodeMap.get( "DESCRIPTION" ));
            map.put("reworkQty" , fqcCheckNc.getReworkQty());
            map.put("scrapQty" , fqcCheckNc.getScrapQty());
            map.put( "createdDateTime" , createdDateTime );

            list.add( map );
        }
        return list;

    }
}
